/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managers;

import entities.Address;
import entities.UserProject;
import interfaces.AddressInterface;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Implementation of AddressInterface
 *
 * @author Thibault
 */
@Stateless
public class AddressManager implements AddressInterface {

    @PersistenceContext(unitName = "project")
    private EntityManager em;

    @Override
    public void createAddress(Address address) {
        em.persist(address);
    }

    @Override
    public Address findById(long id) throws EJBException {
        Address a = em.find(Address.class, id);
        if (a != null) {
            return a;
        } else {
            throw (new EJBException("No Address entity found for id " + id));
        }
    }

    @Override
    public void removeAdress(long id) throws EJBException {
        em.remove(em.find(UserProject.class, id));
    }

    @Override
    public void updateAdress(Address address) {
        em.merge(address);
    }
}
